21. PID Control Example
After all this math, it might be a good idea to recap what we have seen so far through an example to intuitively see how the PID controller works for a quadrotor.
Let’s approach every single parameter independently, so you can see how they affect the behavior of the drone.
In this example, we want to build a controller that allows us to control the altitude of the drone (this is again a one-dimensional world).
As you recall, the diagram for our system is simplified below:
As we have seen earlier in the lessons and the exercises, the input to our system is the thrust we can command to the propellers (generated by the propellers’ speed), and the output is the altitude from the ground.
Conversely, the input of the controller is the measured error, and the output is the thrust that the controller signals to the drone.
The error in our example is the difference between the actual (present) altitude and the target altitude.
Proportional component:
Our starting state sees the drone on the ground, and our goal is to lift the drone up to reach the desired (target) altitude.
To compensate for the error, we will input a thrust (derived from the propellers’ speed), which will allow the drone to lift off and therefore reduce the error.
On the other hand, let’s see what happens when the drone reaches the target altitude:
In this case, since the error is zero, the command to the drone will demand zero thrust, and therefore will stop the propellers’ speed and cause the drone to fall.
As soon as it starts falling, the error increases and the controller will send a signal to increase the thrust, to compensate for that error between the current and the target altitude.
Let’s take a step further and add the goal of hovering the drone to the target location.
Hovering implies a lift thrust equal to the weight of the drone.
Let’s see how the proportional controller influences the behavior of the drone. Let’s assume that the equilibrium (hovering state) is reached when the thrust is equal to 100N.
If we pick a proportional gain (Kp) equal to 1, then the commanded thrust would be equivalent to 100 (error = 100; Kp=1 => thrust = 100*1 = 100N) and the drone would continue to hover at ground level.
If we increase Kp to 5, then the drone will hover at an altitude of 80m, since the drone will reach the commanded thrust when the error is equal to 20m.
We can see that the more we increase Kp, the smaller the error will be.
Unfortunately, this error won’t ever go away. No matter how big Kp is, we won’t be able to get the error to zero. Not cool, right?
So far, the proportional gain hasn’t helped our drone reach the target altitude. It certainly got us closer to it, but it still carries a constant error (called “steady state error”).
The proportional controller uses only present information. We were comparing actual altitude with target altitude. But to help us get rid of this constant error introduced by the proportional gain, we can use an integrator path into our controller.
Integral component
The integrator path (identified by the integral-gain Ki) looks, instead, at information coming from the past. Thus, the integral part keeps in memory what has happened before.
This is how our system diagram looks like with these two controllers running in parallel:
The integrator controller sums up the input signal (the error in our case) over time, and it adjusts the system to counter the accumulation of the error.
A situation of “steady-state” error causes a residual error between current and actual position. This bias keeps the drone from flying at exactly the target altitude, but always a bit off from it.
The integral component looks at the past error, and tries to reduce the accumulated error by signaling an increase of thrust. The more time we spend below our goal, the more thrust will be applied.
While the proportional term adjusts the output based on the present error, the integral term adjusts the output based on past errors.
Therefore, if the drone is flying below the target altitude, then the integral component will provide a thrust higher than 100N (the equilibrium thrust).
To remove the excess of thrust, we will need to have a negative error so that the accumulated sum of error lowers, and the integral component sends a signal to slow down the thrust.
But, to get a negative error, the drone has to overshoot the target, then undershoot a bit until it finally reaches the target altitude.
Although a controller with proportional and integral components can help us eventually reach the target altitude, this overshooting is not ideal, and should be minimized it as much as possible.
It would be great if we had something that could predict the future and tell us how fast we will be approaching the goal in order to regulate it! That’s what the derivative term of our controller does!
Derivative component
The derivative term measures the error’s rate of change. Therefore, it sees how fast the error is increasing/decreasing.
If the error is decreasing quickly, then the rate of change is negative (think about the derivative as the slope of the curve), and the output from our derivative term of the controller is a negative output. This means that it lowers the thrust (essentially the propellers’ speed).
In this case, looking at the rate of change of the error helps the controller determine how the drone is approaching the goal. The controller then slows it down the drone appropriately to prevent overshooting.
These three components (proportional, integral, derivative) contribute to the output of the controller, and we can decide how to weigh each contribution by adjusting the gain terms (Kp, Ki, Kd).